<template>
      <div style="height: 90vh">
          <div class="nav" @click="return_">
              <button class="ret_">返回</button>
          </div>
           <div  class="top_">
               <div class="avatar">
                   <img :src="edit_info.avatar" alt="" >
                    <p style="margin-top: 10px">用户头像</p>
               </div>

                 <ul class="top_ul">
                    <li> 用户ID：<input type="text" v-model="edit_info.uid" disabled></li>
                    <li> 账号：<input type="text" v-model="edit_info.account" disabled></li>
                    <li> 用户名：<input type="text"  v-model="edit_info.username"></li>
                    <li> 电话：<input type="text"  v-model="edit_info.phone"></li>
                    <li> 性别：
                        <label>
                            男<input class="check_" type="radio" @click="sex_(1)" :checked="edit_info.sex === 1">
                        </label>
                        <label>
                            女<input class="check_" type="radio" @click="sex_(0)" :checked="edit_info.sex === 0">
                        </label>
                    </li>
                    <li> 邮箱：<input type="text"  v-model="edit_info.email" :class="{'error_color':email_error}"></li>
                    <li> 出生年月：<input type="date" v-model="edit_info.born" ></li>
                    <li> 年龄：<input type="text" v-model="edit_info.age" disabled></li>
                    <li> 注册日期：<input type="text" v-model="edit_info.registrationTime" disabled></li>
                    <li> 最后一次登录时间：<input type="text" v-model="edit_info.lastLogonTime" disabled></li>
                    <li> 积分：<input type="text" v-model="edit_info.integral"></li>
                    <li> 状态：
                       <label>
                           启用<input class="check_" type="radio" @click="state_(1)" :checked="edit_info.state === 1">
                       </label>
                        <label>
                           禁用<input class="check_" type="radio" @click="state_(0)" :checked="edit_info.state === 0">
                        </label>
                        <label>
                           注销<input class="check_" type="radio" @click="state_(-1)" :checked="edit_info.state === -1">
                        </label>
                    </li>
                     <div class="choose">
                         <button class="save_b" @click="update_">保存</button>
                     </div>
                </ul>

           </div>
      </div>
</template>

<script>
import api from "@/api";
import dayjs from "dayjs";
import {generalTools} from "@/config/util/general";

export default {
    name: "InformationEditing",
    data(){
        return{
            edit_info:{
              uid:"",
              username:"",
              account:"",
              registrationTime:"",
              phone:"",
              born:"",
              avatar:"",
              state:"",
              lastLogonTime:"",
              sex:"",
              motto:"",
              email:'',
              integral:'',
            },
            phone_error:false,
            email_error:false,
        }
    },
    mounted() {
       let edit_info = JSON.parse(window.localStorage.getItem("edit_info"));
        this.edit_info = edit_info
        this.edit_info.age = generalTools.age_(edit_info.born)
    },
    watch:{
        'edit_info.born'(newValue,oldValue){
            this.edit_info.age = generalTools.age_(newValue)
        },
        'edit_info.email'(newValue,oldValue){
            let email = /^([a-zA-Z\d][\w-]{2,})@(\w{2,})\.([a-z]{2,})(\.[a-z]{2,})?$/ ;
            this.email_error = !(email.test(newValue));
        },
        'edit_info.phone'(newValue,oldValue){
            let phone = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
            this.phone_error = !(phone.test(newValue));
        },
    },
    methods:{
        return_(){
            this.$router.go(-1)
        },
        update_(){
            if(this.email_error || this.phone_error){
                this.$store.commit("requestInfo", "参数错误请检查")
                return "";
            }
          api.updateUserInfo(this.edit_info).then((res)=>{
              if(res.data.code === 200){
                  this.$store.commit("requestInfo", "修改成功")
                  this.$router.go(-1)
              }else {
                  this.$store.commit("requestInfo", "修改失败")
              }
          })
        },
        state_(state){
            this.edit_info.state = state;
        },
        sex_(sex){
            this.edit_info.sex = sex;
        },
    },
}
</script>

<style scoped>
input{
    outline: none;
    width: 200px;
    height: 30px;
    border: 0;
    text-indent: 0.5rem;
    border-radius: 2px;
    border-bottom: 1px solid #8f8c8c;
}
.nav{
    display: flex;
    align-items: center;
    font-size: 18px;
    cursor: pointer;
    margin-bottom: 50px;
}
.nav:hover{
    opacity: .9;
}
.top_{
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 18px;
    margin: auto;
}
.top_ul li{
    list-style: none;
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-top: 15px;
}
.top_ul li:nth-child(1){
    margin-top: 0;
}
.choose{
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 30px;
}
.choose button,.ret_{
    border: 0;
    padding: 10px 20px;
    color: white;
    cursor: pointer;
    border-radius: 2px;
}

.choose button:hover{
    opacity: .9;
}
.save_d{
    background: #d06060;
    margin-right: 20px;
}
.ret_{
    padding: 6px 15px;
    background: #3586f1;
    margin-top: 5px;
}
.save_b{
    background: #3586f1;
}
.avatar{
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}
.avatar img{
    object-fit: cover;
    width: 750px;
    aspect-ratio: 16/9;
    border-radius: 3px;
}
.check_{
    width: 13px;

}
label{
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 15px;
    cursor: pointer;
}
.error_color{
    color: #ee4f4f;
}
</style>